package HomeWork20241201;

import java.util.Random;
/*选择排序*/
public class HomeWork3 {
    public static void main(String[] args) {
        int[] arr = getArr();

        System.out.print("排序前:");
        printArr(arr);

        selectSort(arr);

        System.out.print("\n排序后:");
        printArr(arr);
    }

    /**
     * 选择排序
     *
     * @param arr
     */
    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    /**
     * 打印数组
     *
     * @param arr
     */
    public static void printArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    /**
     * 随机生成数组
     *
     * @return 返回数组
     */
    public static int[] getArr() {
        int arr[] = new int[10];

        Random r = new Random();

        for (int i = 0; i < arr.length; i++) {
            int rNum = r.nextInt(100);
            arr[i] = rNum;
        }
        return arr;
    }
}
